package com.tvunetworks.center.user.jms;

import com.tvunetworks.center.user.service.UserDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * @program: tvucc-aggregator
 * @description: MQConsumer
 * @author lebronchen
 * @create: 2019-11-05 14:45
 **/
@Component
@Slf4j
public class MQConsumer {

    @Autowired
    private UserDeviceService userDeviceService;

    @Value("${enableDeviceAuthorizationMQNotify:true}")
    private boolean enableMQNotify;
    @Value("${showDeviceAuthorizationMQLog:true}")
    private boolean showMQLog;
    @Value("${mqRecoveryInterval:1000}")
    private Long mqRecoveryInterval;

//    @Bean("jmsQueueListenerContainerFactory")
//    public JmsListenerContainerFactory jmsTopicListenerContainerFactory(ConnectionFactory connectionFactory) {
//        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
//        factory.setConnectionFactory(connectionFactory);
//        //重连间隔时间
//        factory.setRecoveryInterval(mqRecoveryInterval);
//        factory.setPubSubDomain(true);
//        // 给订阅者一个名字,并开启订阅
//        factory.setClientId(mqClientId);
//        factory.setSubscriptionDurable(false);
//        factory.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
//        return factory;
//    }

    /**
     *
     * @param message message
     */
    @JmsListener(destination = "${deviceAuthorizationMQQueue}"/*, containerFactory = "jmsQueueListenerContainerFactory"*/)
    public void receive(String message) {
        if (!enableMQNotify) {
            return;
        }
        // 新增flag，并且打日志到新的文件中
        if (showMQLog) {
            log.error("deviceAuthorizationMQQueue receive message:{}", message);
        }

        try {
            userDeviceService.notifyDevicePairChange(message);
        } catch (Exception e) {
            log.error(e.toString(), e);
        }


    }

}
